/**
 * Created by stjj on 2022/9/14.
 */

import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.Set;

public class SampleTest1 {
    private AndroidDriver driver;

    @Before
    //夜神：127.0.0.1:62001
    //雷电：emulator:5554
    public void setUp() throws MalformedURLException {
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        desiredCapabilities.setCapability("platformName", "Android");
        desiredCapabilities.setCapability("appium:platformVersion", "7.1.2");
        desiredCapabilities.setCapability("appium:deviceName", "127.0.0.1:62001");
        desiredCapabilities.setCapability("appium:appPackage", "com.mingdao.ews2");
        desiredCapabilities.setCapability("appium:appActivity", "com.dtr.zxing.activity.MainActivity");
        desiredCapabilities.setCapability("appium:noReset", true);
        desiredCapabilities.setCapability("appium:automationName", "UiAutomator1");
        desiredCapabilities.setCapability("appium:ensureWebviewsHavePages", true);
        desiredCapabilities.setCapability("appium:nativeWebScreenshot", true);
        desiredCapabilities.setCapability("appium:newCommandTimeout", 3600);
        desiredCapabilities.setCapability("appium:connectHardwareKeyboard", true);

        URL remoteUrl = new URL("http://127.0.0.1:4723/wd/hub");

        driver = new AndroidDriver(remoteUrl, desiredCapabilities);
    }

    @Test
    public void sampleTest() throws InterruptedException {
        try {
            Thread.sleep(8000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        MobileElement el1 = (MobileElement) driver.findElementByXPath("\t\n" +
                "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.webkit.WebView/android.webkit.WebView/android.view.View[1]/android.view.View[2]/android.view.View[1]/android.view.View[2]/android.widget.EditText");
        el1.click();
        el1.sendKeys("13172247526");
        MobileElement el2 = (MobileElement) driver.findElementByXPath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.webkit.WebView/android.webkit.WebView/android.view.View[1]/android.view.View[2]/android.view.View[2]/android.view.View[2]/android.widget.EditText");
        el2.click();
        el2.sendKeys("Abc123456@");
        MobileElement el3 = (MobileElement) driver.findElementByXPath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.webkit.WebView/android.webkit.WebView/android.view.View[1]/android.view.View[2]/android.widget.Button");
        el3.click();
        TouchAction touchAction = new TouchAction(driver);
        //press：按压某一个坐标 moveTo:滑动到某一个点 release:手指的释放
        Thread.sleep(2000);
        touchAction.press(PointOption.point(461, 1255)).waitAction(WaitOptions.waitOptions(Duration.ofMillis(1500))).moveTo(PointOption.point(461, 619)).release().perform();
        //driver.findElementByAndroidUIAutomator("text(\"明道生产制造厂(培训)\")").click();
        clickListElement();
        Thread.sleep(2000);
        driver.findElementByAndroidUIAutomator("text(\"确认 \")").click();
        Thread.sleep(5000);
        driver.findElementByAndroidUIAutomator("text(\"确认\")").click();
        Thread.sleep(2000);
        Set<String> contextHandles = driver.getContextHandles();
        System.out.println(contextHandles);


        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    @After
    public void tearDown() {
        driver.quit();
    }
    public  void clickListElement(){
        driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textMatches(\"明道生产制造厂(培训)\").instance(0))").click();


    }
}
